package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import ControlLayer.*;//The TUILayer needs to know the ControlLayer
import ModelLayer.*; 

/**
 * User interface class to the AddressBook
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AddressBookUI
{   
    private AddressCtr addrCtr;
    private Person person;
   
     /* Adds the Addrresbook control to the addressbookUI */
    public AddressBookUI(AddressCtr addrCtr)
    {
      this.addrCtr = addrCtr;
    }
     /* Starts the program */
    public void start()
    {
       addressMenu();
    }
     /* All the choices you can make in the program.To delete, get a person, create a person, delete a person and list all persons */
    public void addressMenu()
    {
       boolean exit = false;
       while(!exit) {//that is: while exit is false
           
          int choice = writeAddressBookMenu(); 
          if(choice == 1) {
              getPerson();
          }
          else{
              if(choice == 2) {
                    createPerson();
              }  
              else{
                  if(choice == 3){
                      deletePerson();
                      
                  }  
                  else{
                      if(choice == 4){
                          updatePerson();
                      }
                      else{
                          if(choice == 5){
                              listAllPersons();
                            }
                          else{
                              exit = true;
                           }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
       }//end while
    }
     /* Prints all the choices you can make in the program */
    public int writeAddressBookMenu()
    {      // makes an object keyboard to read input from the screen    
        Scanner keyboard = new Scanner(System.in);     
        System.out.println("\f *** AddressBook Menu ***");          
        System.out.println(" (1) Show information about person");           
        System.out.println(" (2) Add person");           
        System.out.println(" (3) Delete person");          
        System.out.println(" (4) Update person");          
        System.out.println(" (5) List all persons");          
        System.out.println(" (6) Return");          
        System.out.print("\n Make your choice: ");                       
        int choice = keyboard.nextInt();       
        return choice;
    }
        
    //the method reads id for a person and return 
    public long inputPersonsID()
    {   
        boolean ok = false;
        long id = 0;
        while(!ok) {
            // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Id:  ");
           try {
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e) {  
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        }//end while
        return id;
    }
     /* Get's the input for the name */
    public String inputName()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Name:  ");
         String name = keyboard.nextLine();
         return name;
    }
    /* Get's the input for the postal code */
    public String inputPostalCode()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Postal Code:  ");
         String code = keyboard.nextLine();
         return code;
    }
     /* Get's the input for the city */
    public String inputCity()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write City:  ");
         String city = keyboard.nextLine();
         return city;
    }
     /* GEt's the input for the phone */
    public String inputPhone()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Phone Number:  ");
         String phone = keyboard.nextLine();
         return phone;
    } 
      /* Ger's the input for the addressd */
    public String  inputAddress()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Address: ");
        String address = keyboard.nextLine();
        return address;
    }
     /* Shows the ID of the person */
    public void showId(long id)
    {
        System.out.println("ID of the person: " + id);
    }
     /* Shows the name of the person */
    public void showName(String name)
    { 
        System.out.println("The name of the persone: " + name);
    }
     /* Shows the address fo the person */
    public void showAddress(String address)
    { 
        System.out.println("The address of the person: " + address);
    }
  /* Gets the person by ID */
    private void getPerson()
    {
       long id = inputPersonsID();
       addrCtr.getPerson(id);
       Scanner keyboard = new Scanner(System.in); 
       keyboard.nextLine();
    } 
     /* Creates a person */
    private void createPerson()
    {
        long id = inputPersonsID();
        String name = inputName();
        String address = inputAddress();
        String postalCode = inputPostalCode();
        String city = inputCity();
        String phone = inputPhone();
        addrCtr.createPerson(id, name, address, postalCode,city, phone);
    }
     /* Deletes a person */
    private void deletePerson()
    {
        long id = inputPersonsID();
        addrCtr.deletePerson(id);
    }
     /* Updates the person's info */
    private void updatePerson()
    {
        long id = inputPersonsID();
        String name = inputName();
        String address = inputAddress();
        String postalCode = inputPostalCode();
        String city = inputCity();
        String phone = inputPhone();
        addrCtr.updatePerson(id, name, address, postalCode,city, phone);
    }
     /* Lists all info about all persons */
    private void listAllPersons()
    {
       addrCtr.listAllPersons();
       Scanner keyboard = new Scanner(System.in); 
       keyboard.nextLine();
    }
    
}
            
        
